﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DGM.Objects.General;
using DGM.Objects.Msmq;
using DGM.Objects.SocialMedia;
using DGM.Objects.SocialMedia.Twitter;
using DGM.Util.Helpers.Google;
using DGM.Util.Helpers.Klout;
using DGM.Util.Helpers.Msmq;
using DGM.Util.Helpers.UClassify;

namespace DGM.Service.AnalyseService
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Message Analyser");

			_kloutHelper1 = new KloutHelper(KloutHelper.ApiKey1);
			_kloutHelper2 = new KloutHelper(KloutHelper.ApiKey2);
			MsmqManager.ListenToQueueAsync<Tweet>(QueueType.Private, "TwitterQueue", AnalyseTweet);//, "FormatName:DIRECT=OS:24cptl-nstrecke");
			Console.ReadLine();
		}

		private static bool _first = true;
		private static KloutHelper _kloutHelper1;
		private static KloutHelper _kloutHelper2;

		private static void AnalyseTweet(ResultObject<Tweet> tweet)
		{
			if (_first)
				AnalyseTweet(_kloutHelper1, "A", tweet);
			else
				AnalyseTweet(_kloutHelper2, "B", tweet);
			_first = !_first;
		}

		private static void AnalyseTweet(KloutHelper kloutHelper, string proc, ResultObject<Tweet> obj)
		{
			//var language = UClassifyHelper.AnalyseLanguage(obj.Data.Message).GetCurrentLanguage();
			//var message = TranslateHelper.TranslateToEnglish(obj.Data.Message);
			var message = obj.Data.Message;
			var klout = Task.Factory.StartNew(() => kloutHelper.GetKloutScoreByUser(obj.Data.UserName));
			var sentiment = Task.Factory.StartNew(() => UClassifyHelper.AnaliseSentiment(message));
			//var mood = Task.Factory.StartNew(() => UClassifyHelper.AnaliseMood(message));
			Task.WaitAll(klout, sentiment);

		    double kloutScore = klout.Result.Score == 0 ? 10 : klout.Result.Score;

            double calculatedNegative = sentiment.Result.Cls1.Negative * kloutScore * 100.0;
            double calculatedPositive = sentiment.Result.Cls1.Positive * kloutScore * 100.0;

			var group = obj.Data.Message.GetTickers();
			if (!string.IsNullOrEmpty(group))
			{
				var newMsg = new ClassifiedTweet
					{
						Tweet = obj.Data,
						Klout = kloutScore,
						//PositiveMood = mood.Result.Cls1.Happy,
						//NegativeMood = mood.Result.Cls1.Upset,
						PositiveSentiment = sentiment.Result.Cls1.Positive * 100.0,
						NegativeSentiment = sentiment.Result.Cls1.Negative * 100.0,
						CalculatedNegative = calculatedNegative,
						CalculatedPositive = calculatedPositive,
						CategoryGroup = group,
						CalculatedMasterValue = calculatedPositive - calculatedNegative
					};
				Console.WriteLine(proc + " " + obj.Data.UserName + ": " + newMsg.CategoryGroup + " (" + newMsg.Klout + ") " + newMsg.NegativeSentiment + " " + newMsg.PositiveSentiment);
				MsmqManager.SendToQueue(QueueType.Private, "ClassifiedTwitterQueue", newMsg,
				                        proc + "_" + obj.Data.UserName + ": " + newMsg.CategoryGroup);
			}
		}
	}
}
